/*!
 * This is the unit test entry point. It will setup the unit test suite and
 * execute the unit tests.
 *
 *  Copyright (C) 2011  Anthony Wertz.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

 #include <cppunit/extensions/TestFactoryRegistry.h>
 #include <cppunit/CompilerOutputter.h>
 #include <cppunit/TestResult.h>
 #include <cppunit/TestResultCollector.h>
 #include <cppunit/TestRunner.h>
 #include <cppunit/BriefTestProgressListener.h>

/*!
 * Runs the unit tests.
 * @param argc Number of command line arguments.
 * @param argv Pointer to command line arguments.
 * @return Will return non-zero if the unit test execution passes.
 */
int main( int argc, char **argv )
{
  std::string testPath = (argc > 1) ? std::string(argv[1]) : "";

  // Create the event manager and test controller
  CppUnit::TestResult controller;

  // Add a listener that print dots as test run.
  CppUnit::BriefTestProgressListener progress;
  controller.addListener( &progress );

  // Add a listener that colllects test result
  CppUnit::TestResultCollector result;
  controller.addListener( &result );

  // Add the top suite to the test runner
  CppUnit::TestRunner runner;
  runner.addTest( CppUnit::TestFactoryRegistry::getRegistry().makeTest() );

  std::cout
    << std::endl
    << "Running unit tests: " <<  testPath << std::endl
    << std::endl;
  runner.run( controller, testPath );
  std::cout << std::endl;

  // Print test results.
  CppUnit::CompilerOutputter outputter( &result, std::cout );
  outputter.write();

  return result.wasSuccessful() ? 0 : 1;
}
